package com.tjhzzx.ibmsSupervisor.util;

import javax.baja.nre.util.TextUtil;
import javax.baja.sys.BAbsTime;
import javax.baja.sys.BMonth;
import javax.baja.sys.BRelTime;
import javax.baja.timezone.BTimeZone;
import java.io.IOException;

public class TimeUtil {

    public static String getStdTimeString(String datestr, String timestr) {
        String s = "";
        s = s + datestr;
        s = s + "T";
        s = s + timestr;
        s = s + ".000Z";
        return s;
    }

    public static String getStdTimeString(String dateStr) {
        String s = "";
        s = s + dateStr;
        if (dateStr.indexOf(":") == -1) {
            s = s + "T00:00:00";
        } else {
            s = TextUtil.replace(dateStr, " ", "T");
        }
        s = s + ".000Z";
        return s;
    }

    public static String getStdTimeString(BAbsTime time) {
        return getFormatTimeString(time, "YY-MM-DDThh:mm:ss.000Z");
    }

    public static String getFormatTimeString(BAbsTime time, String format) {
        if (time.isNull()) {
            return "";
        }
        String s = format;
        s = TextUtil.replace(s, "YY", time.getYear() + "");
        s = TextUtil.replace(s, "MM", getFullString(time.getMonth().getMonthOfYear()) + "");
        s = TextUtil.replace(s, "DD", getFullString(time.getDay()) + "");
        s = TextUtil.replace(s, "hh", getFullString(time.getHour()) + "");
        s = TextUtil.replace(s, "mm", getFullString(time.getMinute()));
        s = TextUtil.replace(s, "ss", getFullString(time.getSecond()) + "");

        return s;
    }

    private static String getFullString(int i) {
        if (i < 10) {
            return "0" + i;
        }
        return i + "";
    }

    public static BAbsTime getTrueTime(String timeStr, int timeOffset)
            throws IOException {
        if ((timeStr == null) || (timeStr.trim().equals(""))) {
            return BAbsTime.now();
        }
        BAbsTime time = BAbsTime.make(getStdTimeString(timeStr)).subtract(BRelTime.makeHours(timeOffset));

        return time;
    }

    public static BAbsTime getEqualAbsTime(String timeStr) {
        BAbsTime time = BAbsTime.now();
        if ((timeStr != null) && (!timeStr.trim().equals(""))) {
            try {
                time = BAbsTime.make(getStdTimeString(timeStr)).subtract(BRelTime.make(BTimeZone.getLocal().getUtcOffset()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return time;
    }

    public static BAbsTime makeEqualAbsTime(String timeStr) {
        BAbsTime time = BAbsTime.now();
        if ((timeStr != null) && (!timeStr.trim().equals(""))) {
            String[] timeArray = timeStr.split(" ");
            String[] days = timeArray[0].split("-");
            String[] times = timeArray[1].split(":");
            return BAbsTime.make(Integer.parseInt(days[0]),
                    BMonth.make(Integer.parseInt(days[1])-1),
                    Integer.parseInt(days[2]),
                    Integer.parseInt(times[0]),
                    Integer.parseInt(times[1]),
                    Integer.parseInt(times[2]),
                    0,
                    BTimeZone.getLocal());
        }
        return time;
    }

    public static BAbsTime getBqlTime(String timeStr) {
        BAbsTime time = BAbsTime.now();
        if ((timeStr != null) && (!timeStr.trim().equals(""))) {
            try {
                time = BAbsTime.make(getStdTimeString(timeStr)).subtract(BRelTime.make(2 * BTimeZone.getLocal().getUtcOffset()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return time;
    }
}
